package gw.com.android.ui.home;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.presenter.http.HomeTradeRequest;
import gw.com.android.ui.news.TradeNewsActivity;
import gw.com.android.utils.PackStatis;

/**
 * @author jett
 * @since 2017-11-22.
 */
public class HomeTradeNewsView extends LinearLayout {

    private FrameLayout flLoading;
    private LinearLayout llFailure;
    private FrameLayout flCalendar;
    private FrameLayout flFlash;
    private FrameLayout flNews;
    private TextView calendarTitle;
    private RatingBar calendarRatingBar;
    private TextView calendarSubtitle1;
    private TextView calendarSubtitle2;
    private TextView flashContent;
    private TextView newsTitle;
    private TextView newsSubTitle;

    public HomeTradeNewsView(Context context) {
        super(context);
    }

    public HomeTradeNewsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeTradeNewsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        RelativeLayout titleBar = (RelativeLayout) findViewById(R.id.rl_title_bar);
        flLoading = (FrameLayout) findViewById(R.id.fl_loading);
        llFailure = (LinearLayout) findViewById(R.id.ll_failure);
        flCalendar = (FrameLayout) findViewById(R.id.fl_calendar);
        flFlash = (FrameLayout) findViewById(R.id.fl_flash);
        flNews = (FrameLayout) findViewById(R.id.fl_news);

        calendarTitle = (TextView) flCalendar.findViewById(R.id.calendar_title);
        calendarRatingBar = (RatingBar) flCalendar.findViewById(R.id.calendar_rating_bar);
        calendarSubtitle1 = (TextView) flCalendar.findViewById(R.id.calendar_subtitle1);
        calendarSubtitle2 = (TextView) flCalendar.findViewById(R.id.calendar_subtitle2);

        flashContent = (TextView) flFlash.findViewById(R.id.flash_content);

        newsTitle = (TextView) flNews.findViewById(R.id.news_title);
        newsSubTitle = (TextView) flNews.findViewById(R.id.news_subtitle);

        titleBar.setOnClickListener(new TradeClickListener(TradeNewsActivity.CALENDAR, "clicktradeinf"));
        flCalendar.setOnClickListener(new TradeClickListener(TradeNewsActivity.CALENDAR, "clickcalendar"));
        flFlash.setOnClickListener(new TradeClickListener(TradeNewsActivity.FLASH, "clicknflash"));
        flNews.setOnClickListener(new TradeClickListener(TradeNewsActivity.TOPNEWS, "clickFnews"));
    }

    private class TradeClickListener implements View.OnClickListener {
        private int page;
        private String eventAction;

        TradeClickListener(int page, String eventAction) {
            this.page = page;
            this.eventAction = eventAction;
        }

        @Override
        public void onClick(View v) {
            PackStatis.getToService(eventAction, PackStatis.EventCategory.HOME.getValue(), null, null);
            getContext().startActivity(new Intent(getContext(), TradeNewsActivity.class).putExtra(TradeNewsActivity.PAGE_INDEX, page));
        }
    }

    public void refresh(HomeTradeRequest.HomeTradeData data) {
        flLoading.setVisibility(GONE);
        if (data == null) {
            llFailure.setVisibility(VISIBLE);
            flCalendar.setVisibility(GONE);
            flFlash.setVisibility(GONE);
            flNews.setVisibility(GONE);
            return;
        }
        if (!data.hasCalendar && !data.hasFlash && !data.hasMain) {
            return;
        }
        llFailure.setVisibility(GONE);
        if (data.hasCalendar) {
            flCalendar.setVisibility(VISIBLE);
            calendarTitle.setText(data.calendarTitle);
            String forecast = getResources().getString(R.string.home_trade_news_forecast) + (TextUtils.isEmpty(data.calendarForecast) ? "--" : data.calendarForecast);
            String preview = getResources().getString(R.string.home_trade_news_preview) + (TextUtils.isEmpty(data.calendarPreview) ? "--" : data.calendarPreview);
            calendarSubtitle1.setText(forecast);
            calendarSubtitle2.setText(preview);
            calendarRatingBar.setRating(data.calendarRating);
        } else {
            flCalendar.setVisibility(GONE);
        }

        if (data.hasFlash) {
            flFlash.setVisibility(VISIBLE);
            flashContent.setText(data.flashContent);
        } else {
            flFlash.setVisibility(GONE);
        }

        if (data.hasMain) {
            flNews.setVisibility(VISIBLE);
            newsTitle.setText(data.mainTitle);
            newsSubTitle.setText(data.mainContent);
        } else {
            flNews.setVisibility(GONE);
        }
    }

}
